<?php
/* 
 * Copyright (c) 2011 Antoine d'Otreppe de Bouvette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

class URLWriter {
    /**
     *
     * @var Request
     */
    private $request;
    
    private $routes = array();
    
    public function __construct(array $routes) {
        foreach ($routes as $name=>$pattern) {
            $this->routes[$name] = $pattern;
        }
    }
    
    public function urlTo($route, $object) {
        if (is_array($object)) {
            $object = new ArrayTransformer($object);
        }
        elseif ($object !== null && !is_object($object)) {
            throw new RuntimeException(
                "Argument given to \$urlTo must be an array " .
                "or an object");
        }
        
        if (array_key_exists($route, $this->routes)) {
            $call = method_exists($object, '__call');
            return $this->request->getWebsiteAddress() .
                   preg_replace_callback(AspyctURL::URL_PARAM_PATTERN,
                    function ($match) use ($object, $route, $call) {
                        $method = 'get'.ucfirst($match['name']);
                        
                        if ($call || method_exists($object, $method)) {
                            return $object->$method();
                        }
                        else {
                            throw new RuntimeException(
                                "Missing $method() on " . get_class($object) .
                                " for route '$route'");
                        }
                    },
                $this->routes[$route]);
        }
        else {
            throw new RuntimeException(
                "No such route: " . $route);
        }
    }
    
    public function urlToResource($path) {
        return $this->getRequest()->getWebsiteAddress()->getDirectory() . $path;
    }
    
    /**
     *
     * @return Request
     */
    public function getRequest() {
        return $this->request;
    }

    /**
     *
     * @param Request $request 
     */
    public function setRequest(Request $request) {
        $this->request = $request;
    }
}

function url_to($route, $param=null) {
    return Environment::getInstance()->urlWriter->urlTo($route, $param);
}

function url_to_resource($path) {
    return Environment::getInstance()->urlWriter->urlToResource($path);
}
